﻿/*========================== 
 * @author 郑金泉
 * @desc 实体Bool字段类
 * ========================= */
using System;
using System.Collections.Generic;
using System.Text;

namespace Obsidian.Edm
{
    public class BoolField : ModelField, IModelField
    {

        public override FieldType Type
        {
            get { return FieldType.Bool; }
        }
        
        private bool _value;
        /// <summary>
        /// 获取值
        /// </summary>
        public bool Value
        {
            get 
            {
                if (_isNull)
                    throw new Exception("空值状态无法获得该属性值");
                return this._value; 
            }
        }

        /// <summary>
        /// 获取值
        /// </summary>
        public object OValue
        {
            get 
            {
                if (_isNull) return null;
                else return _value;
            }
        }

        /// <summary>
        /// 设置值
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public IModel Set(bool value)
        {
            if (!base._assigned)
                base._assigned = true;
            this._isNull = false;
            this._value = value;
            return base.Model;
        }

        /// <summary>
        /// 设置值
        /// </summary>
        public IModel Set(object value)
        {
            if (value == null)
                return this.SetNull();

            bool boolVal = Convert.ToBoolean(value);
            return this.Set(boolVal);
        }

        /// <summary>
        /// 设置值
        /// </summary>
        public bool TrySet(object value)
        {
            if (value == null)
            {
                this.SetNull();
                return true;
            }

            try
            {
                this.Set(Convert.ToBoolean(value));
                return true;
            }
            catch
            {
                this.SetNull();
                return false;
            }

        }

        /// <summary>
        /// 设置为空值状态
        /// </summary>
        public IModel SetNull()
        {
            this._value = false;
            this._isNull = true;
            return base.Model;
        }

        public BoolField(IModel model)
        {
            this.InitField(model);
        }

        public BoolField(IModel model, string dbFieldName)
        {
            this.InitField(model, dbFieldName);
        }

        public BoolField(IModel model, string dbFieldName, string alias)
        {
            this.InitField(model, dbFieldName, alias);
        }

        public BoolField(IModel model, string dbFieldName, string alias, bool defaultValue)
        {
            this.InitField(model, dbFieldName, alias);
            this._value = defaultValue;
            this._isNull = false;
        }

        /// <summary>
        /// 从Int类型转化为值(true:大于0; false:小于等于0)
        /// </summary>
        /// <param name="val"></param>
        public void FromInt(int val)
        {
            this._value = val > 0;
        }

        /// <summary>
        /// 转化为Int类型(true:1;false:0)
        /// </summary>
        /// <returns></returns>
        public int ToInt()
        {
            return this._value ? 1 : 0;
        }

        public string ToString()
        {
            return _isNull ? null : _value.ToString();
        }

        public bool isTrue()
        {
            if (this.IsNull)
                return false;
            return this._value == true;
        }

        public bool isFalse()
        {
            if (this.IsNull)
                return false;
            return this._value == false;
        }

    }
}
